package com.kzy.demoadmin.web.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kzy.demoadmin.web.entity.Notice;
import com.kzy.demoadmin.web.mapper.NoticeMapper;
import com.kzy.demoadmin.web.service.NoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
@Slf4j
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 根据id查询
     * @param integer
     * @return
     */
    @Override
    public Notice findById(Integer integer) {
        return noticeMapper.findById(integer);
    }

    /**
     * 分页查询
     * @param notice
     * @return
     */
    @Override
    public PageInfo<Notice> getNoticeList(Notice notice) {
        PageHelper.startPage(notice.getPageNo(), notice.getPageSize());
        List<Notice> list = noticeMapper .searchBy(notice);
        return new PageInfo<>(list);
    }

    /**
     * 删除审批
     * @param id
     * @return
     */
    @Override
    public int deleteById(Integer id) {
        return noticeMapper.deleteById(id);
    }

    /**
     * 新增日志
     * @param notice
     */
    @Override
    public void add(Notice notice) {
        //设置时间
        notice.setTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        noticeMapper.insert(notice);
    }

    /**
     * 修改日志
     * @param notice
     */
    @Override
    public void edit(Notice notice) {
        //设置时间
        notice.setTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        noticeMapper.update(notice);
    }
}
